package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamUserPracticeRecordVo;
import com.ruoyi.exam.service.IExamUserPracticeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户刷题记录Controller
 *
 * @author Tellsea
 * @date 2025-06-23
 */
@Api(tags = "用户刷题记录Controller")
@RestController
@RequestMapping("/exam/userPracticeRecord")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamUserPracticeRecordController extends BaseController {

    private final IExamUserPracticeRecordService examUserPracticeRecordService;

    @ApiOperation("查询用户刷题记录列表")
    @PreAuthorize("@ss.hasPermi('exam:userPracticeRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamUserPracticeRecordVo> list(ExamUserPracticeRecordVo entity) {
        return examUserPracticeRecordService.queryList(entity);
    }

    @ApiOperation("查询用户刷题记录所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamUserPracticeRecordVo entity) {
        return AjaxResult.success("查询成功", examUserPracticeRecordService.queryAll(entity));
    }

    @ApiOperation("导出用户刷题记录列表")
    @PreAuthorize("@ss.hasPermi('exam:userPracticeRecord:export')")
    @Log(title = "用户刷题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamUserPracticeRecordVo entity) {
        List<ExamUserPracticeRecordVo> list = examUserPracticeRecordService.queryAll(entity);
        ExcelUtil<ExamUserPracticeRecordVo> util = new ExcelUtil<>(ExamUserPracticeRecordVo.class);
        util.exportExcel(response, list, "用户刷题记录数据");
    }

    @ApiOperation("获取用户刷题记录详细信息")
    @PreAuthorize("@ss.hasPermi('exam:userPracticeRecord:query')")
    @GetMapping(value = "/getInfo/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return AjaxResult.success("查询成功", examUserPracticeRecordService.queryById(recordId));
    }

    @ApiOperation("新增用户刷题记录")
    @PreAuthorize("@ss.hasPermi('exam:userPracticeRecord:add')")
    @Log(title = "用户刷题记录", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamUserPracticeRecordVo entity) {
        return toAjax(examUserPracticeRecordService.save(entity));
    }

    @ApiOperation("修改用户刷题记录")
    @PreAuthorize("@ss.hasPermi('exam:userPracticeRecord:edit')")
    @Log(title = "用户刷题记录", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamUserPracticeRecordVo entity) {
        return toAjax(examUserPracticeRecordService.updateById(entity));
    }

    @ApiOperation("删除用户刷题记录")
    @PreAuthorize("@ss.hasPermi('exam:userPracticeRecord:remove')")
    @Log(title = "用户刷题记录", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(examUserPracticeRecordService.removeByIds(Arrays.asList(recordIds)) ? 1 : 0);
    }

    @ApiOperation("获取用户题库的题目答题情况和进度信息")
    @GetMapping("/bankQuestionPracticeInfo/{bankId}")
    public AjaxResult getBankQuestionPracticeInfo(@PathVariable("bankId") Long bankId) {
        Long userId = getUserId();
        return AjaxResult.success("查询成功", examUserPracticeRecordService.getBankQuestionPracticeInfo(userId, bankId));
    }

    @ApiOperation("获取用户题库的错题信息")
    @GetMapping("/bankWrongQuestionInfo/{bankId}")
    public AjaxResult getBankWrongQuestionInfo(@PathVariable("bankId") Long bankId) {
        Long userId = getUserId();
        return AjaxResult.success("查询成功", examUserPracticeRecordService.getBankWrongQuestionInfo(userId, bankId));
    }

    @ApiOperation("获取当前用户所有题库的刷题进度")
    @GetMapping("/myBankProgressInfo")
    public AjaxResult myBankProgressInfo() {
        Long userId = getUserId();
        return AjaxResult.success("查询成功", examUserPracticeRecordService.getAllBankProgressInfo(userId));
    }

    @ApiOperation("重置指定题库的刷题进度与记录")
    @PostMapping("/resetBankProgress/{bankId}")
    public AjaxResult resetBankProgress(@PathVariable("bankId") Long bankId) {
        Long userId = getUserId();
        return toAjax(examUserPracticeRecordService.resetBankProgress(userId, bankId));
    }
}
